/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.Command;
import com.sun.lwuit.Form;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import core.MessageFormPool;
import core.MessageManager;
import core.ScreenEngine;
import datamodel.Message;
import view.additional.CellItem;
import view.additional.ThumbList;
import view.additional.TickerRenderer;
import view.commands.BackCommand;

/**
 *
 * @author Kvest
 */
public class MessagesListForm extends Form
{
    private static final int SHOW_COMMAND_ID = 1;
    
    private ThumbList items;
    private Command show;
    
    public MessagesListForm()
    {
        super("messages");

        init();
    }
     
    private void init()
    {
        setLayout(new BorderLayout());
        setScrollable(false);
        
        //Создаем список
        items = new ThumbList();
        items.setSelectCommandText("...");
        items.setFixedSelection(ThumbList.FIXED_NONE_CYCLIC);
        TickerRenderer render = new TickerRenderer();
        items.setRenderer(render);

        //Добавляем список на форму
        addComponent(BorderLayout.CENTER, items);
        
        //Команды
        show = new Command("show", SHOW_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
                selectAction();

                evt.consume();
            }
        };
        addCommand(show);

        items.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent evt) 
            {
                show.actionPerformed(evt);
            }
        });
        
        addCommand(new BackCommand());
    }
    
    public void updateContent()
    {
        //Чистим форму
        clear();
        
        //Заполняем список
        MessageManager messageManager = MessageManager.getManager();
        Message message;
        for (int i = messageManager.getCount() - 1; i >= 0; --i)
        {
            message = messageManager.getMessageByIndex(i);        
            items.addItem(new CellItem(message.getId(), message.getTitle()));
        }
    }
    
    private void clear()
    {
        while (items.getModel().getSize() > 0)
        {
            items.getModel().removeItem(0);
        }
    }
    
    public void show()
    {
        //Обновляем содержимое формы
        updateContent();

        super.show();
    }
    
    private void selectAction()
    {
        CellItem selected = (CellItem)items.getSelectedItem();
        if (selected != null)
        {
            Message message = MessageManager.getManager().getMessage(selected.getId());
            
            if (message != null)
            {
                //Отображаем сообщение
                showMessageForm(message);
            }
        }
    }
    
    private void showMessageForm(Message message)
    {
        //Получаем форму
        MessageForm messageForm = MessageFormPool.getPool().getMessageForm();
       
        //Устанавливаем сообщение
        messageForm.setContent(message);
        
        //Отображаем
        ScreenEngine.getEngine().push(messageForm);
    }
}
